package com.jrfid.smartcabinet.demo.utils;

import android.annotation.SuppressLint;
import android.graphics.ImageFormat;
import android.media.Image;

import androidx.annotation.NonNull;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageProxy;

import java.util.concurrent.locks.ReentrantLock;

public class test2 implements ImageAnalysis.Analyzer {
    private byte[] y;
    private byte[] u;
    private byte[] v;
    private ReentrantLock lock = new ReentrantLock();
    private Object mImageReaderLock = 1;//1 available,0 unAvailable
    private long lastDrawTime = 0;
    private int timerSpace = 300; // 识别间隔
    private byte[] nv21;

    @Override
    public void analyze(@NonNull ImageProxy imageProxy) {
        @SuppressLint("UnsafeOptInUsageError")
        Image mediaImage = imageProxy.getImage();
//            int rotationDegrees = imageProxy.getImageInfo().getRotationDegrees();
        if (mediaImage != null) {
            synchronized (mImageReaderLock) {
                /*识别频率Start 和状态*/
                long start = System.currentTimeMillis();
                if (start - lastDrawTime < timerSpace || !mImageReaderLock.equals(1)) {
                    mediaImage.close();
                    imageProxy.close();
                    return;
                }
                lastDrawTime = System.currentTimeMillis();
                /*识别频率End*/

                //判断YUV类型，我们申请的格式类型是YUV_420_888
                if (ImageFormat.YUV_420_888 == mediaImage.getFormat()) {
                    Image.Plane[] planes = mediaImage.getPlanes();
                    lock.lock();
                    if (y == null) {
                        y = new byte[planes[0].getBuffer().limit() - planes[0].getBuffer().position()];
                        u = new byte[planes[1].getBuffer().limit() - planes[1].getBuffer().position()];
                        v = new byte[planes[2].getBuffer().limit() - planes[2].getBuffer().position()];
                    }
                    //从planes中分别获取y、u、v 变量数据
                    if (mediaImage.getPlanes()[0].getBuffer().remaining() == y.length) {
                        planes[0].getBuffer().get(y);
                        planes[1].getBuffer().get(u);
                        planes[2].getBuffer().get(v);

                        if (nv21 == null) {
                            nv21 = new byte[planes[0].getRowStride() * mediaImage.getHeight() * 3 / 2];
                        }

                        if (nv21.length != planes[0].getRowStride() * mediaImage.getHeight() * 3 / 2) {
                            return;
                        }
                        // 回传数据是YUV422
                        if (y.length / u.length == 2) {
                            ImageUtils.yuv422ToYuv420sp(y, u, v, nv21, planes[0].getRowStride(), mediaImage.getHeight());
                        }
                        // 回传数据是YUV420
                        else if (y.length / u.length == 4) {
                            nv21 = ImageUtils.yuv420ToNv21(mediaImage);
                        }

                    }
                    lock.unlock();
                }
            }
        }

        //一定要关闭
        mediaImage.close();
        imageProxy.close();

    }
}
